/*********************************************************************************
 *      Copyright:  (C) 2020 makun<1394987689@qq.com>
 *                  All rights reserved.
 *
 *       Filename:  conf.c
 *    Description:  This file 
 *                 
 *        Version:  1.0.0(2020年07月08日)
 *         Author:  makun <1394987689@qq.com>
 *      ChangeLog:  1, Release initial version on "2020年07月08日 23时17分49秒"
 *                 
 ********************************************************************************/

#include "iniparser.h"
#include "conf.h"

int gain_mqtt_conf(char *path_ini, mqtt_ctx_t *mqtt)
{
    dictionary    *ini = NULL;
    const char    *hostname;
    int           port;
    const char    *username;
    const  char   *passwd;
    const  char   *clientid;
    const  char   *pubTopic;
    int           pubQos;
    int           keepalive;

    const   char *method;
    const   char *id;
    const   char *identifier;
    const   char *version;

    if(!path_ini || !mqtt)
    {   
        printf("Invalid input argument in %s!\n",__FUNCTION__);
        return -1; 
    }   

    /* 加载配置文件，将数据存于dictionary结构中*/
    ini = iniparser_load(path_ini);
    if(ini == NULL)
    {   
        printf("iniparser_load failure!\n");
        return -2; 
    }


    /*获取字符串，若未找到返回第三个参数的内容*/
    hostname = iniparser_getstring(ini,"aly_address:host", DEF_BORKER_HOSTNAME);
    port = iniparser_getint(ini,"aly_address:port", DEF_BORKER_PORT);

    username = iniparser_getstring(ini, "user_passwd:username", DEF_BORKER_USERNAME);
    passwd = iniparser_getstring(ini, "user_passwd:passwd", DEF_BORKER_PASSWD);

    clientid = iniparser_getstring(ini, "clientId:id",DEF_BORKER_CLIENTID );

    pubTopic = iniparser_getstring(ini, "pub_topic:topic", DEF_BORKER_PUBTOPIC);


    pubQos = iniparser_getint(ini, "aly_Qos:QoS", DEF_PUBQOS);

    keepalive = iniparser_getint(ini, "KEEP_ALIVE:alive",DEF_BROKER_KEEPALIVE);

    method = iniparser_getstring(ini, "aly_json:method",DEF_METHOD);
    id = iniparser_getstring(ini, "aly_json:id",DEF_ID);
    identifier = iniparser_getstring(ini, "aly_json:identifier",DEF_IDENTIFIER);
    version = iniparser_getstring(ini, "aly_json:version",DEF_VERSION);

    strncpy(mqtt->method, method, sizeof(mqtt->method));
    strncpy(mqtt->id, id, sizeof(mqtt->method));
    strncpy(mqtt->identifier, identifier,sizeof(mqtt->identifier));
    strncpy(mqtt->version, version, sizeof(mqtt->version));



    /*  Broker settings  */
    strncpy(mqtt->hostname, hostname, sizeof(mqtt->hostname) );
    mqtt->port = port;
    printf("Use default broker server [%s:%d]\n", mqtt->hostname, mqtt->port);

    strncpy(mqtt->username, username, sizeof(mqtt->username) );
    strncpy(mqtt->passwd , passwd, sizeof(mqtt->passwd) );
    printf("Use default broker author by [%s:%s]\n", mqtt->username, mqtt->passwd);

    mqtt->keepalive= keepalive;
    printf("Use default broker keepalive timeout [%d] seconds\n", mqtt->keepalive);

    /*  Publisher settings  */
    strncpy(mqtt->pubTopic, pubTopic, sizeof(mqtt->pubTopic));
    mqtt->pubQos = pubQos;
    printf( "Use default publisher topic \"%s\" with Qos[%d]\n",mqtt->pubTopic, mqtt->pubQos);

    strncpy(mqtt->clientid,  clientid, sizeof(mqtt->clientid));

    /*释放dictionary对象/内存*/
    iniparser_freedict(ini); 

    return 0;

}

int set_mqtt_conf(char *path_ini, char *host, int port, char *clientid, char *user, char *passwd, char *topic)
{
    FILE        *fp = NULL;
    dictionary  *ini = NULL;
    char        * mqtt_port;
    char          mqtt_pot[16];

    if(port)
    {
        snprintf(mqtt_pot, sizeof(mqtt_pot), "%d" , port);
        mqtt_port = mqtt_pot;
    }

    ini = iniparser_load(path_ini);

    if(ini == NULL)
    {
        printf("iniparser failure \n");
        return -1;
    }
    /*配置参数*/
    iniparser_set(ini, "aly_address:host", host);
    iniparser_set(ini, "aly_address:port", mqtt_port);
    iniparser_set(ini, "clientId:id", clientid);
    iniparser_set(ini, "user_passwd:username", user);
    iniparser_set(ini, "user_passwd:passwd", passwd);
    iniparser_set(ini, "pub_topic:topic", topic);
    /*有些参数后面再配置*/

    fp = fopen(path_ini, "w");
    if(fp == NULL)
    {
        printf("stone: fopen error!\n");
        return -2;
    }
    
    iniparser_dump_ini(ini, fp);

    iniparser_freedict(ini);

    return 0;
}
